package com.eric.windows;

import android.annotation.TargetApi;
import android.graphics.PorterDuff;
import android.os.Build;
import android.os.Bundle;
import android.app.Activity;
import android.os.SystemClock;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AnalogClock;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.Chronometer;
import android.widget.CompoundButton;
import android.widget.DigitalClock;
import android.widget.Gallery;
import android.widget.ImageView;
import android.widget.Spinner;
import android.widget.Switch;
import android.widget.Toast;
import android.widget.ToggleButton;

import com.eric.windows.adapter.GalleryAdapter;


public class ToggleSwitchActivity extends Activity  {


    @TargetApi(Build.VERSION_CODES.M)
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_toggle_switch);

        final Switch switch_ = (Switch) findViewById(R.id.switch_);
        final Switch switch_2 = (Switch) findViewById(R.id.switch_2);
        //设置开关图标上文本的样式
        switch_.setSwitchTextAppearance(this,R.style.switch_style);
        //设置开关的最小宽度
        switch_.setSwitchMinWidth(50);
        //设置标题与文本之间的空白
        switch_.setSwitchPadding(10);
        switch_.setText("off");
//        switch_.setTextOn("");

        //设置监听
        switch_.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked){
                    switch_.setText("open");
                    Toast.makeText(ToggleSwitchActivity.this, "open", Toast.LENGTH_SHORT).show();
                }else {
                    switch_.setText("off");
                    Toast.makeText(ToggleSwitchActivity.this, "off", Toast.LENGTH_SHORT).show();
                }
            }
        });

        //使用自定义Drawable绘制开关按钮
        switch_2.setThumbDrawable(getResources().getDrawable(R.drawable.thumb));
        //使用自定义Drawable绘制开关轨道
        switch_2.setTrackDrawable(getResources().getDrawable(R.drawable.track));
//        switch_2.setThumbTintMode(PorterDuff.Mode.CLEAR);




        AnalogClock analogClock = (AnalogClock) findViewById(R.id.an_clock);

        DigitalClock digitalClock  = (DigitalClock) findViewById(R.id.dig_clock);


        final Chronometer  chronometer  = (Chronometer) findViewById(R.id.chron);
        ToggleButton toggleButton  = (ToggleButton) findViewById(R.id.tog_btn);
        toggleButton.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked){
                    //设置计时时间
                    chronometer.setBase(SystemClock.elapsedRealtime());
                    //开始计时
                    chronometer.start();
                    chronometer.setEnabled(false);
                }else {
                    //结束计时
                    chronometer.stop();
                    chronometer.setEnabled(true);
                    Toast.makeText(ToggleSwitchActivity.this, "时长:" + chronometer.getText(), Toast.LENGTH_SHORT).show();
                }
            }
        });

        //设置监听事件
        chronometer.setOnChronometerTickListener(new Chronometer.OnChronometerTickListener() {
            @Override
            public void onChronometerTick(Chronometer chronometer) {
                //
                if (SystemClock.elapsedRealtime() - chronometer.getBase() > 20*1000){
                    chronometer.stop();
                    chronometer.setEnabled(true);
                    Toast.makeText(ToggleSwitchActivity.this, "时长:" + chronometer.getText(), Toast.LENGTH_SHORT).show();
                }
            }
        });


        Spinner spinner = (Spinner) findViewById(R.id.spinner);
        String[] strings = {"猪八戒","孙悟空","唐僧"};
        ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(this,R.layout.item_spinner,strings);
        spinner.setAdapter(arrayAdapter);


        Gallery gallery  = (Gallery) findViewById(R.id.gallery);
        //设置动画时间
        gallery.setAnimationDuration(5000);
        //设置未选中图片的透明度
        gallery.setUnselectedAlpha(0.6f);
        final ImageView imgVIew  = (ImageView) findViewById(R.id.imgVIew);
        imgVIew.setImageResource(R.mipmap.ic_launcher);
        imgVIew.setScaleType(ImageView.ScaleType.FIT_XY);
        //图片资源
        final int[] imgIds = {R.mipmap.pic_1,R.mipmap.pic_2,R.mipmap.pic_3,R.mipmap.pic_4,R.mipmap.pic_5};
        GalleryAdapter adapter  = new GalleryAdapter(this,imgIds);
        gallery.setAdapter(adapter);

        gallery.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                imgVIew.setImageResource(imgIds[position]);
            }
        });


    }




}
